package com.example.myapplication;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;

/**
 * Created by xiaolong on 2022/1/8 1:13 下午
 * email：xinxiaolong123@foxmail.com
 */
public class JetpackApplication extends Application {


    private int version=2;

    private static JetpackApplication jetpackApplication;

    private static final String TAG="JetpackApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        jetpackApplication=this;
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new ApplicationObserver());

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                       Log.e("JetpackApplication","onActivityCreated："+activity);
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                Log.e("JetpackApplication","onActivityStarted："+activity);
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                Log.e("JetpackApplication","onActivityResumed："+activity);
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                Log.e("JetpackApplication","onActivityPaused："+activity);
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                Log.e("JetpackApplication","onActivityStopped："+activity);
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
    }

    private static class ApplicationObserver implements LifecycleObserver {

        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        private void onAppBackground() {
            Log.w(TAG, "[LifecycleChecker]: app moved to background");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        private void onAppForeground() {
            Log.w(TAG, "[LifecycleChecker]: app moved to foreground");
        }
    }

    public static JetpackApplication getInstance(){
        return jetpackApplication;
    }

}
